// Module: bbo_track (with packed arrays)
// Description: Detects changes in Best Bids and Offers (BBO) for all contracts.
//              Triggers Fair Value (FV) recalculation when the main instrument changes.

module bbo_track #(
    parameter int NUM_CONTRACTS = 3
) (
    input  logic                         clk_net,
    input  logic                         rst_n,

    input  logic [NUM_CONTRACTS-1:0][31:0] bbo_bidprice,
    input  logic [NUM_CONTRACTS-1:0][31:0] bbo_askprice,
    input  logic [NUM_CONTRACTS-1:0][7:0]  bbo_bidvolume,
    input  logic [NUM_CONTRACTS-1:0][7:0]  bbo_askvolume,
    input  logic [NUM_CONTRACTS-1:0]       bbo_changed,

    output logic [NUM_CONTRACTS-1:0][31:0] current_bidprice,
    output logic [NUM_CONTRACTS-1:0][31:0] current_askprice,
    output logic [NUM_CONTRACTS-1:0][7:0]  current_bidvolume,
    output logic [NUM_CONTRACTS-1:0][7:0]  current_askvolume,
    output logic                           fv_recalc_trigger
);

    // Detect whether there has been a change in price for any of type of apples
    integer i;
    always_ff @(posedge clk_net) begin
        if (!rst_n) begin
            for (i = 0; i < NUM_CONTRACTS; i++) begin
                current_bidprice[i]  <= '0;
                current_askprice[i]  <= '0;
                current_bidvolume[i] <= '0;
                current_askvolume[i] <= '0;
            end
            fv_recalc_trigger <= 1'b0;
        end else begin
            if (bbo_changed[0]) fv_recalc_trigger <= 1;
            else fv_recalc_trigger <= 0;
            for (i = 0; i < NUM_CONTRACTS; i++) begin
                if (bbo_changed[i]) begin
                    current_bidprice[i]  <= bbo_bidprice[i];
                    current_askprice[i]  <= bbo_askprice[i];
                    current_bidvolume[i] <= bbo_bidvolume[i];
                    current_askvolume[i] <= bbo_askvolume[i];
                end
                // Else no updates
            end
        end
    end

endmodule
